package zhangyi.tdd.ecommerce.domain;

import zhangyi.tdd.ecommerce.domain.exception.InvalidOrderException;
import zhangyi.tdd.ecommerce.port.client.InventoryCheckingClient;

public class OrderService {
    private InventoryCheckingClient inventoryClient;

    public OrderService(InventoryCheckingClient inventoryClient) {
        this.inventoryClient = inventoryClient;
    }

    public void validate(Order order) {
        order.validate();
        validateInventory(order);
    }

    private void validateInventory(Order order) {
        InventoryReview inventoryReview = inventoryClient.check(order);
        boolean isAvailable = inventoryReview.isAvailable();
        if (!isAvailable) {
            throw new InvalidOrderException("Not enough inventory quantity");
        }
    }
}
